package Servlet;

import Dao.UserDao;
import Entity.User;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

@WebServlet(name = "ServletLogup", value = "/logup")
public class ServletLogup extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        Map<String,String> map = null;
        try {
            map = UserDao.queryall();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        if(!map.containsKey(username)){
            System.out.println(map.containsKey(username));
            try {
                UserDao.save(new User(username,password));
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            request.setAttribute("username", username);
            request.getRequestDispatcher("home.jsp").forward(request, response);
        }
        else{
            request.getRequestDispatcher("logup.jsp").forward(request, response);
        }
    }
}
